package net.wanho.shop.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.shop.entity.User;
import net.wanho.shop.service.UserService;
import net.wanho.shop.vo.Page;
import net.wanho.shop.vo.R;
import net.wanho.shop.vo.UserVo;
import org.springframework.web.bind.annotation.*;

/**
 * Author：汤小洋
 * Date：2025-09-16 15:50
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping("/register")
    public R register(@RequestBody User user){
        userService.register(user);
        return R.success("注册成功");
    }

    @PostMapping("/login")
    public R login(@RequestBody User user){
        UserVo userVo = userService.login(user);
        return R.success(userVo);
    }

    @GetMapping("/userName/{userName}")
    public R getUserByUserName(@PathVariable String userName){
        User user = userService.getUserByUserName(userName);
        return R.success(user);
    }

    @GetMapping("/page")
    public R findByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        User user){
        Page<User> page = userService.findByPage(pageNum, pageSize,user);
        return R.success(page);
    }
}
